#pragma once

#include <map>
#include "timer.h"
#include "singleton.h"

using namespace yx;

namespace yx
{
	namespace timeline
	{
		class TimerManager
		{
			friend class pattern::Singleton<TimerManager>;
		public:
			/*template<typename F,typename... Args>
			void schedule(int milliseconds, F&& f, Args&& ...args);*/

			template<typename F, typename... Args>
			void schedule(int repeat, int milliseconds, F&& f, Args&& ...args);

			void update();

		private:
			TimerManager() = default;
			~TimerManager() = default;
			TimerManager(const TimerManager&) = delete;
			TimerManager operator=(const TimerManager&) = delete;

			std::multimap<int64_t, timeline::Timer> _timers;
		};


		/*template<typename F, typename... Args>
		void TimerManager::schedule(int milliseconds, F&& f, Args&& ...args)
		{
			schedule(milliseconds, -1, std::forward<F>(f), std::forward<Args>(args)...);
		}*/

		template<typename F, typename... Args>
		void TimerManager::schedule(int repeat, int milliseconds, F&& f, Args&& ...args) 
		{
			timeline::Timer t(repeat);

			t.callback(milliseconds, std::forward<F>(f), std::forward<Args>(args)...);

			_timers.insert(std::make_pair(t._time, t));
		}
	}
}